<template>
  <div class="dashboard-container">
    <div class="dashboard-header">
      <div class="header-left">
        <div class="title-box">
          <h1>沙扒湾旅游实时监控中心</h1>
          <div class="title-decoration"></div>
        </div>
        <div class="update-time">
          <span class="time-label">数据更新时间：</span>
          <span class="time-value">{{ currentTime }}</span>
        </div>
      </div>
      <div class="header-right">
        <div class="weather-info">
          <el-icon><Sunny /></el-icon>
          <span>28°C</span>
          <span>晴朗</span>
        </div>
      </div>
    </div>
    
    <div class="stat-cards">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card" :class="{ 'animate': stats.todayVisitors > 0 }">
            <div class="card-header">
              <span class="card-title">今日游客</span>
              <el-icon class="card-icon"><Location /></el-icon>
            </div>
            <div class="card-body">
              <div class="card-value">
                <transition name="fade" mode="out-in">
                  <span :key="stats.todayVisitors">{{ stats.todayVisitors }}</span>
                </transition>
              </div>
              <div class="card-footer">
                <span class="trend" :class="stats.visitorChange >= 0 ? 'up' : 'down'">
                  <el-icon><component :is="stats.visitorChange >= 0 ? 'ArrowUp' : 'ArrowDown'" /></el-icon>
                  {{ Math.abs(stats.visitorChange) }}%
                </span>
                <span class="trend-label">较昨日</span>
              </div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card" :class="{ 'animate': stats.ticketIncome > 0 }">
            <div class="card-header">
              <span class="card-title">门票收入</span>
              <el-icon class="card-icon"><Ticket /></el-icon>
            </div>
            <div class="card-body">
              <div class="card-value">
                <transition name="fade" mode="out-in">
                  <span :key="stats.ticketIncome">¥{{ stats.ticketIncome }}</span>
                </transition>
              </div>
              <div class="card-footer">
                <span class="trend up">
                  <el-icon><ArrowUp /></el-icon>
                  12.5%
                </span>
                <span class="trend-label">较昨日</span>
              </div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card" :class="{ 'animate': stats.onlineBookings > 0 }">
            <div class="card-header">
              <span class="card-title">在线预订</span>
              <el-icon class="card-icon"><User /></el-icon>
            </div>
            <div class="card-body">
              <div class="card-value">
                <transition name="fade" mode="out-in">
                  <span :key="stats.onlineBookings">{{ stats.onlineBookings }}</span>
                </transition>
              </div>
              <div class="card-footer">
                <span class="trend up">
                  <el-icon><ArrowUp /></el-icon>
                  8.3%
                </span>
                <span class="trend-label">较昨日</span>
              </div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card" :class="{ 'animate': stats.hotSpots > 0 }">
            <div class="card-header">
              <span class="card-title">景点热度</span>
              <el-icon class="card-icon"><View /></el-icon>
            </div>
            <div class="card-body">
              <div class="card-value">
                <transition name="fade" mode="out-in">
                  <span :key="stats.hotSpots">{{ stats.hotSpots }}</span>
                </transition>
              </div>
              <div class="card-footer">
                <span class="trend up">
                  <el-icon><ArrowUp /></el-icon>
                  15.2%
                </span>
                <span class="trend-label">较昨日</span>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    
    <div class="dashboard-content">
      <el-row :gutter="20">
        <el-col :xs="24" :lg="16">
          <div class="chart-card">
            <div class="card-header">
              <div class="header-left">
                <span class="title">游客流量趋势</span>
                <div class="decoration-line"></div>
              </div>
              <el-radio-group v-model="chartTimeRange" size="small">
                <el-radio-button label="day">今日</el-radio-button>
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
              </el-radio-group>
            </div>
            <div class="chart-container">
              <div ref="visitorChart" class="chart"></div>
            </div>
          </div>
        </el-col>
        
        <el-col :xs="24" :lg="8">
          <div class="realtime-card">
            <div class="card-header">
              <div class="header-left">
                <span class="title">实时监控</span>
                <div class="decoration-line"></div>
              </div>
            </div>
            <div class="monitor-list">
              <div v-for="(item, index) in realtimeData" :key="index" class="monitor-item">
                <div class="monitor-icon" :class="item.type">
                  <el-icon><component :is="item.icon" /></el-icon>
                </div>
                <div class="monitor-content">
                  <div class="monitor-title">{{ item.title }}</div>
                  <div class="monitor-value">
                    <transition name="fade" mode="out-in">
                      <span :key="item.value">{{ item.value }}%</span>
                    </transition>
                  </div>
                </div>
                <div class="monitor-chart">
                  <mini-line-chart :data="item.trend" :color="item.color" />
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { Location, Ticket, User, View, Sunny, ArrowUp, ArrowDown } from '@element-plus/icons-vue'
import * as echarts from 'echarts'
import MiniLineChart from '@/components/MiniLineChart.vue'

const router = useRouter()
const chartTimeRange = ref('day')
const visitorChart = ref(null)
let chart = null

// 统计数据
const stats = ref({
  todayVisitors: 0,
  visitorChange: 0,
  ticketIncome: 0,
  onlineBookings: 0,
  hotSpots: 0
})

// 实时数据
const realtimeData = ref([
  { 
    title: '停车场使用率', 
    value: '75', 
    type: 'parking', 
    icon: 'Van',
    color: '#409EFF',
    trend: [30, 40, 35, 50, 49, 60, 70, 75]
  },
  { 
    title: '餐厅上座率', 
    value: '60', 
    type: 'restaurant', 
    icon: 'Food',
    color: '#67C23A',
    trend: [20, 30, 40, 35, 45, 50, 55, 60]
  },
  { 
    title: '酒店入住率', 
    value: '85', 
    type: 'hotel', 
    icon: 'House',
    color: '#E6A23C',
    trend: [50, 60, 65, 70, 75, 80, 82, 85]
  },
  { 
    title: '景点承载率', 
    value: '45', 
    type: 'spot', 
    icon: 'Location',
    color: '#F56C6C',
    trend: [20, 25, 30, 35, 40, 42, 43, 45]
  }
])

// 当前时间
const currentTime = ref('')
const currentDate = ref('')

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleTimeString('zh-CN', { 
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
  currentDate.value = now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 初始化图表
const initChart = () => {
  if (visitorChart.value) {
    chart = echarts.init(visitorChart.value)
    const option = {
      backgroundColor: 'transparent',
      tooltip: {
        trigger: 'axis',
        backgroundColor: 'rgba(0,0,0,0.7)',
        borderColor: 'rgba(255,255,255,0.2)',
        textStyle: {
          color: '#fff'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['00:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'],
        axisLine: {
          lineStyle: {
            color: 'rgba(255,255,255,0.2)'
          }
        },
        axisLabel: {
          color: 'rgba(255,255,255,0.7)'
        }
      },
      yAxis: {
        type: 'value',
        axisLine: {
          show: false
        },
        axisLabel: {
          color: 'rgba(255,255,255,0.7)'
        },
        splitLine: {
          lineStyle: {
            color: 'rgba(255,255,255,0.1)'
          }
        }
      },
      series: [{
        name: '游客数量',
        type: 'line',
        smooth: true,
        symbol: 'circle',
        symbolSize: 8,
        data: [120, 132, 101, 134, 90, 230, 210, 182],
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0,
            color: 'rgba(64,158,255,0.3)'
          }, {
            offset: 1,
            color: 'rgba(64,158,255,0.1)'
          }])
        },
        lineStyle: {
          width: 3,
          color: '#409EFF'
        },
        itemStyle: {
          color: '#409EFF',
          borderWidth: 2,
          borderColor: '#fff'
        }
      }]
    }
    chart.setOption(option)
  }
}

// 更新统计数据
const updateStats = () => {
  stats.value = {
    todayVisitors: Math.floor(Math.random() * 1000) + 500,
    visitorChange: Math.floor(Math.random() * 20) - 10,
    ticketIncome: Math.floor(Math.random() * 50000) + 10000,
    onlineBookings: Math.floor(Math.random() * 100) + 50,
    hotSpots: Math.floor(Math.random() * 10) + 1
  }
}

// 初始化
onMounted(() => {
  updateTime()
  setInterval(updateTime, 1000)
  updateStats()
  setInterval(updateStats, 5000)
  initChart()
  window.addEventListener('resize', () => {
    chart && chart.resize()
  })
})

onUnmounted(() => {
  chart && chart.dispose()
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 24px;
  background-color: #0f1c3c;
  min-height: 100vh;
  color: #fff;
  background-image: 
    radial-gradient(circle at 10% 20%, rgba(64,158,255,0.1) 0%, transparent 20%),
    radial-gradient(circle at 90% 80%, rgba(103,194,58,0.1) 0%, transparent 20%);
}

.dashboard-header {
  margin-bottom: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .header-left {
    .title-box {
      position: relative;
      margin-bottom: 12px;
      
      h1 {
        font-size: 32px;
        color: #fff;
        margin: 0;
        font-weight: 600;
        background: linear-gradient(90deg, #409EFF, #67C23A);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        text-shadow: 0 0 20px rgba(64,158,255,0.3);
      }
      
      .title-decoration {
        position: absolute;
        bottom: -8px;
        left: 0;
        width: 100%;
        height: 2px;
        background: linear-gradient(90deg, #409EFF, #67C23A);
        opacity: 0.5;
      }
    }
    
    .update-time {
      font-size: 14px;
      color: rgba(255,255,255,0.7);
      
      .time-label {
        margin-right: 8px;
      }
      
      .time-value {
        color: #409EFF;
      }
    }
  }
  
  .header-right {
    .weather-info {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 16px;
      background: rgba(255,255,255,0.1);
      border-radius: 20px;
      
      :deep(svg) {
        font-size: 20px;
        color: #E6A23C;
      }
      
      span {
        color: rgba(255,255,255,0.9);
      }
    }
  }
}

.stat-cards {
  margin-bottom: 24px;
  
  .stat-card {
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    padding: 20px;
    height: 160px;
    transition: all 0.3s;
    position: relative;
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: linear-gradient(45deg, transparent, rgba(255,255,255,0.1), transparent);
      transform: translateX(-100%);
      transition: 0.5s;
    }
    
    &.animate:hover::before {
      transform: translateX(100%);
    }
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      
      .card-title {
        font-size: 16px;
        color: rgba(255,255,255,0.7);
      }
      
      .card-icon {
        font-size: 24px;
        color: #409EFF;
      }
    }
    
    .card-body {
      .card-value {
        font-size: 36px;
        font-weight: bold;
        color: #fff;
        margin-bottom: 12px;
        text-shadow: 0 0 10px rgba(64,158,255,0.3);
      }
      
      .card-footer {
        display: flex;
        align-items: center;
        gap: 8px;
        
        .trend {
          display: flex;
          align-items: center;
          gap: 4px;
          padding: 4px 8px;
          border-radius: 4px;
          font-size: 14px;
          
          &.up {
            background: rgba(103,194,58,0.2);
            color: #67C23A;
          }
          
          &.down {
            background: rgba(245,108,108,0.2);
            color: #F56C6C;
          }
        }
        
        .trend-label {
          font-size: 14px;
          color: rgba(255,255,255,0.5);
        }
      }
    }
  }
}

.dashboard-content {
  .chart-card, .realtime-card {
    background: rgba(255,255,255,0.05);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 24px;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      
      .header-left {
        display: flex;
        align-items: center;
        gap: 12px;
        
        .title {
          font-size: 18px;
          font-weight: 600;
          color: #fff;
        }
        
        .decoration-line {
          width: 40px;
          height: 2px;
          background: linear-gradient(90deg, #409EFF, transparent);
        }
      }
    }
  }
  
  .chart-container {
    height: 400px;
    
    .chart {
      width: 100%;
      height: 100%;
    }
  }
  
  .monitor-list {
    .monitor-item {
      display: flex;
      align-items: center;
      padding: 16px;
      border-bottom: 1px solid rgba(255,255,255,0.1);
      
      &:last-child {
        border-bottom: none;
      }
      
      .monitor-icon {
        width: 40px;
        height: 40px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 16px;
        
        :deep(svg) {
          font-size: 20px;
          color: white;
        }
        
        &.parking {
          background: linear-gradient(135deg, #409EFF 0%, #85c7ff 100%);
        }
        
        &.restaurant {
          background: linear-gradient(135deg, #67C23A 0%, #95d475 100%);
        }
        
        &.hotel {
          background: linear-gradient(135deg, #E6A23C 0%, #f3c894 100%);
        }
        
        &.spot {
          background: linear-gradient(135deg, #F56C6C 0%, #f89898 100%);
        }
      }
      
      .monitor-content {
        flex: 1;
        
        .monitor-title {
          font-size: 14px;
          color: rgba(255,255,255,0.7);
          margin-bottom: 4px;
        }
        
        .monitor-value {
          font-size: 24px;
          font-weight: bold;
          color: #fff;
        }
      }
      
      .monitor-chart {
        width: 100px;
        height: 40px;
      }
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .dashboard-container {
    padding: 16px;
  }
  
  .dashboard-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .stat-cards {
    .stat-card {
      margin-bottom: 16px;
    }
  }
  
  .dashboard-content {
    .chart-card {
      .chart-container {
        height: 300px;
      }
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: all 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateY(10px);
}

.fade-enter-to,
.fade-leave-from {
  opacity: 1;
  transform: translateY(0);
}
</style>